﻿<?php
/**
 * 通用分页函数
 * @param	String	$msg			弹出的消息 
 * @param	String	$type			类型,-1表示点击后上一页，1表示点击进入自定义页面
 * @param	String	$url			URL
 * @return	String	$message
 * @author	hongweizhiyuan 2014-12-20
 */
function showMsg($msg,$type,$href){
	$message  = "<link href='http://cdn.bootcss.com/bootstrap/3.3.1/css/bootstrap.min.css' rel='stylesheet'>";
	$message .= "<table class='table table-striped table-bordered' style='width:450px;margin:0 auto;margin-top:100px;' width='300' align='center'>\n\t<tr><td height=35 align='center'><strong>提示信息</strong></td></tr>\n\t";
	$message .= "<tr>\n\t<td align='center' style='font-size:12px;'>".$msg."<br />";
	$href = empty($href) ? $_SERVER['HTTP_REFERER'] : $href;
	switch ($type){
		case -1:
			$message .= "<a href='javascript:history.go(-1)'>点击这里返回上一页</a>";
			break;
		case 0:
		case 1:
			$message .= "<a href=\"".$href."\">如果您的浏览器没有跳转，请点击这里</a>";
			$message .= "<script type='text/javascript'>setTimeout('window.location.href='".$href."',3000);</script>\n";
			break;
	}
	$message .= "</td></tr></table>";
	return $message;
	exit();
}

//返回弹出框
function msg($msg,$url=''){

        echo "<script language=JavaScript type=text/JavaScript>";
        echo "window.alert('$msg');";
        if($url=="history") 
        	echo "history.go(-1)";
        elseif($url!="") 
        	echo "document.location='$url';";
        echo "</script>";
}

//返回灯箱效果弹出框
function msgbox($mwidth,$mheight,$mtitle,$mcontent,$mbackurl,$isbox=1){
$str=' <div id="lightshow" class="div_content">';
$str.='    <div  class="div_messageshow"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="94%" id="messagebox" style="padding-left:10px;">&nbsp;</td><td width="6%"><a href="#" onClick="closedivshow()" ><img src="js/Lightbox/img/close1.png" width="14" height="14" border="0"/></a></td></tr></table></div>';
$str.='    <div class="div_main"><table width="100%" border="0" cellpadding="0" cellspacing="0" style="margin-top:10px;"><tr><td>'.$mcontent.'</td></tr></table>';
if($isbox==1){
$str.='         <table width="60" border="0" cellpadding="0" cellspacing="0" style="margin-top:10px;"><tr><td height="20" bgcolor="#990000" align="center"><a href="" id="goback" style="text-decoration: none;"><div class="msgbutton">确 定</div></a></td></tr></table>';
}
$str.='    </div>';
$str.='</div>';
$str.='<div id="fadeshow" class="div_overlay"></div>';
$str.='<script>opendivshow("'.$mwidth.'","'.$mheight.'","'.$mtitle.'","'.$mbackurl.'")</script>';
echo $str;
}

//URL跳转
function gowin($link,$top='n'){ //err
        echo "<script language=JavaScript type=text/JavaScript>";
        if($top=='y'){
        echo "top.document.location.replace('$link');";
        }else{
        echo "document.location.replace('$link');";
        }
        echo "</script>";
}
function AddZero($sourceNumber){
    $newNumber = substr(strval($sourceNumber+1000),1,3);
    return $newNumber;
}


/** 
* 二维数组根据关键字排序函数。 
* @example echo array_sort($array,'price'); 
* @author zhangyl 
*/
function array_sort($arr,$keys,$type='asc'){
    $keysvalue = $new_array = array();
    foreach ($arr as $k=>$v){
             $keysvalue[$k] = $v[$keys];
    }
    if($type == 'asc'){
             asort($keysvalue);
    }else{
             arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k=>$v){
            $new_array[$k] = $arr[$k];
    }
    return $new_array;
}
/** 
* 年龄计算器1，根据填入的 MYSQL DATE 类型的生日计算出当前的年龄。 
* @example echo get_age('1987-05-23'); 
* @author zhangyl 
* @link http://itbdw.tk/ 
* @param $birthday 
*/
function get_age($birthday = '2011-01-01') { 
  
   list($biry, $birm, $bird) = explode('-', $birthday); 
  
   //get current year, month and day 
   $y = date('Y'); 
   $m = date('m'); 
   $d = date('d'); 
  
   $age = $y - $biry; 
  
   if ($m < $birm) { 
       $age--; 
   } elseif ($m = $birm) { 
   if ($d < $bird) { 
       $age--; 
   } 
   } 
  
   return $age; 
} 

//通过年龄反算生日年份
function get_birth($age) { 
  
   //get current year, month and day 
   $y = date('Y'); 
  
   $myyear = $y - $age-1; 
  
   $mybirth=$myyear.'-'.'01-01';
  
   return $mybirth; 
}

//返回错误信息
function showError($err){
	echo "<div class=\"error\"><span>$err</span><br /><a href=\"javascript:history.back();\">返回上一页</a></div>";
	exit();
}

//获取6位随机码
function getRand(){
        $randStr = str_shuffle('1234567890');
        $rand = substr($randStr,0,6);
	return $rand ;
}

//数字转星期
function daxie($number){ 
$number=substr($number,0,2); 
$arr=array("日","一","二","三","四","五","六","日","八","九"); 
if(strlen($number)==1){  
$result=$arr[$number];  
}else{   
if($number==10){    
$result="十";   
}else{    
if($number<20){    
$result="十";    
}else{    
$result=$arr[substr($number,0,1)]."十";    
}    
if(substr($number,1,1)!="0"){    
$result.=$arr[substr($number,1,1)];     
}   
} } 
return $result;
}

//完美字符截取函数
function web_substr($string, $start,$sublen,$endstr='...', $code = 'UTF-8')
{
   if($code == 'UTF-8')
   {
     $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
     preg_match_all($pa, $string, $t_string);
    
     if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen)).$endstr;
     return join('', array_slice($t_string[0], $start, $sublen));
   }else{
     $start = $start*2;
     $sublen = $sublen*2;
     $strlen = strlen($string);
     $tmpstr = '';
     for($i=0; $i<$strlen; $i++)
     {
       if($i>=$start && $i<($start+$sublen))
       {
         if(ord(substr($string, $i, 1))>129) $tmpstr.= substr($string, $i, 2);
         else $tmpstr.= substr($string, $i, 1);
       } 
       if(ord(substr($string, $i, 1))>129) $i++;
     }
     if(strlen($tmpstr)< $strlen ) $tmpstr.= $endstr;
     return $tmpstr;
   }
} 
//指定字数分割字符串（实现对中英文混杂字符进行分割）
/*
  $str = '我爱北京3我爱上海-我爱xianggang';
  $r = mbStrSplit($str, 4);
  print_r($r);
  运行结果：Array([0] => 我爱北京[1] => 3我爱上[2] => 海-我爱[3] => xian[4] => ggan[5] => g)
*/
function mbStrSplit ($string, $len=1) {
     $start = 0;
	 $strlen = mb_strlen($string);
	 while ($strlen) {
	          $array[] = mb_substr($string,$start,$len,"utf8");
			  $string = mb_substr($string, $len, $strlen,"utf8");
			  $strlen = mb_strlen($string);
			  }
	return $array;
}
//权限检测函数
function rights($type){
    global $mysql;
	$result = $mysql->select('z8ar_admin','',' where aid='.$_SESSION['z8ar']['aid']);
    $record = $mysql->fetch($result,'1');
	    if($record['rights1']){
	             $val=explode(',',$record['rights1']);
	             foreach ($val as $nav){
		         $rights[$nav]=$nav;
		         }
				 }
	     if($rights[$type]!=$type)
	     return 0;
		 else
		 return 1;
}
function SendSms($mobile,$content){
$result=file_get_contents("http://www.z8ar.cn/sms/api/?user=99shahua&pass=99shahua&mobile=".$mobile."&content=".$content);
return $result;
}
//PHP SOCKET通讯
function Boss_Api($in_data,$ip,$prot){
error_reporting(0);
set_time_limit(0);
$service_port = $prot;
$address = $ip;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,array("sec"=>60, "usec"=>0 ) );
if ($socket < 0)
{
   echo "socket_create() failed: reason: " . socket_strerror($socket) . "\n";
}
$result = socket_connect($socket, $address, $service_port);
if ($result < 0)
   {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror($result) . "\n";
   }
$in = $in_data;
$out = '';
$out1='';
if(!socket_write($socket, $in, strlen($in)))
   {
    echo "socket_write() failed: reason: " . socket_strerror($socket) . "\n";
   }
   while($out = socket_read($socket, 8192))
   {
    //echo "Server say:<br>";
    return $out;
   }
   socket_close($socket);
}

//翻页函数
function PageData($sql,$params=array(),$PageSize=15)
{
	global $mysql;
	$url="";
	foreach($params as $keys=>$val)
		$url.="&".$keys."=".$val;	
	$recordCount=$mysql->getOne($sql);
	if(!isset($_REQUEST["page"])) $_REQUEST["page"]=1;
	$page=(empty($_REQUEST["page"])?1:$_REQUEST["page"]);
	$pageCount=ceil($recordCount/$PageSize);
	$pageStart=($page-1)*$PageSize;
	$html='总计<font color="#FF0000">'.$recordCount.'</font>条记录 分页:<font color="#FF0000">'.$page.'</font>/<font color="#FF0000">'.$pageCount.'</font>&nbsp;&nbsp;&nbsp;&nbsp;  <a href="?page=1'.$url.'">首页</a>&nbsp; ';
	if($page>=2)
		$html.="<a href=\"?page=".($page-1).$url."\">上一页</a>&nbsp; ";
	else
		$html.="上一页&nbsp; ";
	if($page<$pageCount)
		$html.="<a href=\"?page=".($page+1).$url."\">下一页</a>&nbsp; ";
	else
		$html.="下一页&nbsp; ";
	if($pageCount>=1)	
		$html.="&nbsp; <a href=\"?page=".$pageCount.$url."\">最未页</a>";
	else
		$html.="&nbsp;最未页";
	//$html.='&nbsp;&nbsp;&nbsp;&nbsp;跳转：<input type="text" name="page" style="width:35px;height:18px;line-height:18px;" />&nbsp;&nbsp;<a href="#" onClick="document.aa.submit();">GO</a></form>';
	return array("pageStart"=>$pageStart,"pageSize"=>$PageSize,"html"=>$html);
}

//翻页函数
function myPageData($sql,$params=array(),$PageSize=20,$page=1)
{
	global $mysql;
	$url="";
	foreach($params as $keys=>$val)
		$url.="&".$keys."=".$val;	
	$recordCount=$mysql->getOne($sql);
	$pageCount=ceil($recordCount/$PageSize);
	$pageStart=($page-1)*$PageSize;
	return array("recordCount"=>$recordCount,"page"=>$page,"pageCount"=>$pageCount,"PageSize"=>$PageSize,"pageStart"=>$pageStart,"pageSize"=>$PageSize,"html"=>$html);
}

function PageData_news($sql,$params=array(),$incid,$PageSize=24)
{
	global $mysql;
	$url="";
	foreach($params as $keys=>$val)
		$url.="&".$keys."=".$val;	
	$recordCount=$mysql->getOne($sql);
	if(!isset($_REQUEST["page"])) $_REQUEST["page"]=1;
	$page=(empty($_REQUEST["page"])?1:$_REQUEST["page"]);
	$pageCount=ceil($recordCount/$PageSize);
	$pageStart=($page-1)*$PageSize;
	$html='<form name="aa" action="?1=1'.$url.'" method="post">总计<font color="#FF0000">'.$recordCount.'</font>条记录 分页:<font color="#FF0000">'.$page.'</font>/<font color="#FF0000">'.$pageCount.'</font>&nbsp;&nbsp;&nbsp;&nbsp;  <a href="?page=1'.$url.'">首页</a>&nbsp; ';
	if($page>=2)
		$html.="<a href=\"?page=".($page-1).$url."&cid=".$incid."\">上一页</a>&nbsp; ";
	else
		$html.="上一页&nbsp; ";
	if($page<$pageCount)
		$html.="<a href=\"?page=".($page+1).$url."&cid=".$incid."\">下一页</a>&nbsp; ";
	else
		$html.="下一页&nbsp; ";
	if($pageCount>=1)	
		$html.="&nbsp; <a href=\"?page=".$pageCount.$url."\">最未页</a>";
	else
		$html.="&nbsp;最未页";
	return array("pageStart"=>$pageStart,"pageSize"=>$PageSize,"html"=>$html);
}



?>